<?php
/**************************************************************************************************
|
| AudioShare - Audio Sharing Script
| http://www.audiosharescript.com
| webmaster@audiosharescript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.audiosharescript.com/eula.html and to be bound by it.
|
| Copyright (c) 2010 AudioShareScript.com. All rights reserved.
|**************************************************************************************************/

include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

$mainmenu = "8";
STemplate::assign('mainmenu',$mainmenu);


//Get Variables
$playlistID=(int)$_GET[ID];
$getpage = htmlentities(strip_tags($_REQUEST[page]));
$page = escape($getpage);
STemplate::assign('playlistID',$playlistID);

$userID=0;
if ($_SESSION[USERID] != "" && $_SESSION[USERID] >= 0 && is_numeric($_SESSION[USERID])){
	$userID=$_SESSION[USERID];
}
STemplate::assign('userID',$userID);

## remove songs from the playlist from here...
$deleteMsg="";
if(isset($_POST["audio"]) && !empty($_POST["audio"]) ){

	$audioStr="";
	$audioSelected=$_POST[audio];
	$audioCount=count($audioSelected);
	$audioStr=implode(",",$audioSelected);
	if($audioStr!=""){
		$query="DELETE FROM playlist_audios where PLAYLISTID='".$playlistID."' and AUDIOID in (".$audioStr.") and USERID='".$userID."'";
		$executequery=$conn->execute($query);
		
		## create the updated songs playlist xml...
		create_playlist_xml($playlistID);
		## new playlist xml file created...
		
		$songVar="song";
		if($audioCount>1)
			$songVar="songs";
		$deleteMsg=$audioCount." ".$songVar." removed successfully from the playlist.";
	}
}
## songs removed form the playlist

if($playlistID!="0" && $playlistID!="")
{
	if($page=="")
	{
		$page = "1";
	}
	$currentpage = $page;

	if ($page >=2)
	{
		$pagingstart = ($page-1)*$config[items_per_page];
	}
	else
	{
		$pagingstart = "0";
	}
	
	## query to get playlist details
	$query="select * from playlists where PLAYLISTID='".$playlistID."'";
	$executequery=$conn->execute($query);
	$playlist = $executequery->getrows();
	STemplate::assign('playlist',$playlist);
	
	
	$query1 = "SELECT count(*) as total from playlist_audios pa left join audios a on (a.AUDIOID=pa.AUDIOID) left join playlists p on (p.PLAYLISTID=pa.PLAYLISTID) where pa.PLAYLISTID='".mysql_real_escape_string($playlistID)."' limit $config[maximum_results]";
	$query2 = "SELECT p.playlist_title, a.AUDIOID,a.title,a.artist,a.audio_name,a.ext from playlist_audios pa left join audios a on (pa.AUDIOID=a.AUDIOID) left join playlists p on (pa.PLAYLISTID=p.PLAYLISTID) where pa.PLAYLISTID='".mysql_real_escape_string($playlistID)."'  limit $pagingstart, $config[items_per_page]";

	$executequery1=$conn->execute($query1);
	$getcount = $executequery1->fields[total];

	$showtitle = "view playlist";
	$showseotitle = "View Playlist";

	STemplate::assign('showtitle',$showtitle);
	STemplate::assign('pagetitle',$showtitle);
	STemplate::assign('showseotitle',$showseotitle);
	STemplate::assign('filter',$filter);

	$executequery1 = $conn->Execute($query1);

	$totalaudios = $executequery1->fields['total'];
	if ($totalaudios > 0)
	{
		if($executequery1->fields['total']<=$config[maximum_results])
		{
			$total = $executequery1->fields['total'];
		}
		else
		{
			$total = $config[maximum_results];
		}

		$toppage = ceil($total/$config[items_per_page]);
		if($toppage==0)
		{
			$xpage=$toppage+1;
		}
		else
		{
			$xpage = $toppage;
		}
		$executequery2 = $conn->Execute($query2);
		$audios = $executequery2->getrows();
		
		$beginning=$pagingstart+1;
		$ending=$pagingstart+$executequery2->recordcount();

		$pagelinks="";
		$k=1;
		$theprevpage=$currentpage-1;
		$thenextpage=$currentpage+1;

		if ($currentpage > 0)
		{	
			## paging starts here...
			
			$pagelinks.='<table width="100%" cellspacing="2" cellpadding="2" border="0">';
				$pagelinks.='<tr>';
					$pagelinks.='<td width="33%">';
					$pagelinks.='<a href="javascript:void(0);"><img height="16" width="16" border="0" align="absmiddle" src="'.$config[imageurl].'/ico_rss_s.gif" alt="RSS"  /></a>'; 
						$pagelinks.='<span class="pag"> page '.$currentpage.' of '.$total.'</span>';
					$pagelinks.='</td>';	
					$pagelinks.='<td width="33%" align="right">';
						$pagelinks.="<div class=\"pagination\">";
						if($currentpage > 1) 
						{
							$pagelinks.="<a href='$thebaseurl/view_playlist.php?ID=$playlistID&page=1'>$lang[611]</a>";
							$pagelinks.="<span class='ellipsis'>...</span>";
							$pagelinks.="<a href='$thebaseurl/view_playlist.php?ID=$playlistID&page=$theprevpage'>&laquo; $lang[612]</a>";
						}
						else
						{
							$pagelinks.="<span>&lt;</span>";
						}
						$counter=0;
						$lowercount = $currentpage-5;
						if ($lowercount <= 0) $lowercount = 1;
						
						while ($lowercount < $currentpage)
						{
							$pagelinks.="<a href='$thebaseurl/view_playlist.php?ID=$playlistID&page=$lowercount'>$lowercount</a>";
							$lowercount++;
							$counter++;
						}
						
						$pagelinks.="<span class='current'><span>$currentpage</span></span>";
						
						$uppercounter = $currentpage+1;
						
						while (($uppercounter < $currentpage+10-$counter) && ($uppercounter<=$toppage))
						{
							$pagelinks.="<a href='$thebaseurl/view_playlist.php?ID=$playlistID&page=$uppercounter'>$uppercounter</a>";
							$uppercounter++;
						}
						
						if($currentpage < $toppage) 
						{
							$pagelinks.="<a href='$thebaseurl/view_playlist.php?ID=$playlistID&page=$thenextpage'>$lang[613] &raquo;</a>";
							$pagelinks.="<span class='ellipsis'>...</span>";
							$pagelinks.="<a href='$thebaseurl/view_playlist.php?ID=$playlistID&page=$toppage'>$lang[614]</a>";
						}
						else
						{
							$pagelinks.="<span class=\"disable\">&gt;</span>";
						}
						$pagelinks.="</div>";
					$pagelinks.='</td>';	
				$pagelinks.='</tr>';
			$pagelinks.="</table>";
			## paging ends here
		}
	}
	else
	{
		$error = "There are no audios in this playlist.";
	}
}else{
	$error="No such playlist exists.";
}
STemplate::assign('error',$error);

//TEMPLATES BEGIN
STemplate::assign('deleteMsg',$deleteMsg);
STemplate::assign('beginning',$beginning);
STemplate::assign('ending',$ending);
STemplate::assign('pagelinks',$pagelinks);
STemplate::assign('total',$total);
STemplate::assign('listaudios',$audios);
STemplate::display('header.tpl');
STemplate::display('view_playlist.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>